package com.zhkc.iess.online.test01.config;

import com.zhkc.iess.online.test01.handler.SubscribeHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class NettyServerInitializer2 extends ChannelInitializer<SocketChannel> {

    private PortDefinition portDefinition;

    public NettyServerInitializer2(PortDefinition portDefinition) {
        this.portDefinition = portDefinition;
    }

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        //获取SocketChannel的端口，判断通讯协议
        int port = ch.localAddress().getPort();
        String type = portDefinition.getPort().get(port);
        //不同的请求，匹配不同的handler
        SubscribeHandler handler = SubscribeFactoryConfig.getByName(type);
        handler.handle(pipeline);
    }
}